package com.aiit.error;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:GlobalException
 * Package:com.xu.error
 * Description:
 *异常捕获的两种方式：1.返回JSON 2.返回视图
 * @Date:2020/3/17 15:32
 * @Author:xjs
 */
@Slf4j
@ControllerAdvice(basePackages = {"com.aiit"})//AOP技术（面向切面）
public class GlobalException {

    /**
     * 密码错误
     * @param ex
     * @return 返回到error.html
     */
    @ExceptionHandler(IncorrectCredentialsException.class) //
    public ModelAndView IncorrectCredentialsException(Exception ex) {
        log.info("ex+"+123132123);
        ModelAndView modelAndView=new ModelAndView();
        Map map =new HashMap();
            map.put("code",300);
            map.put("message","用户名/密码错误");
            modelAndView.addObject("map",map);
            modelAndView.setViewName("error");//通过setViewName()方法跳转到指定的页面
            return  modelAndView;
    }

    /**
     * 用户名不存在时异常
     * @param ex
     * @return 返回到error.html
     */
    @ExceptionHandler(UnknownAccountException.class)
    public ModelAndView UnknownAccountException(Exception ex) {
        ModelAndView modelAndView=new ModelAndView();
        Map map =new HashMap();
            map.put("code",3604);
            map.put("message","用户名不存在");
            modelAndView.addObject("map",map);
            modelAndView.setViewName("error");//通过setViewName()方法跳转到指定的页面
            return  modelAndView;

    }

}
